package com.ext.aic.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.ContentCenterListAdapter;
import com.ext.aic.apis.content.ProductListRequest;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.beans.params.ProductListParams;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.ContentCenterLayoutBinding;
import com.ext.aic.enums.SearchSource;
import com.ext.aic.handler.PageHandler;
import com.ext.aic.ui.SearchActivity;
import com.ext.aic.utils.RedirectUtils;
import com.eyinfo.android_pure_utils.ebus.SubscribeEBus;

import java.util.Arrays;
import java.util.List;

public class ContentCenterFragment extends BaseFragment {

    private ContentCenterLayoutBinding binding;
    private ContentCenterListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_center_layout, container, false);
        binding = ContentCenterLayoutBinding.bind(view);
        bindView();
        return binding.getRoot();
    }

    private void bindView() {
        binding.topView.searchAreaRl.setOnClickListener(this::onSearchClick);
        pageHandler.registerListener(binding.refreshListSrl);
    }

    public void onSearchClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("source", SearchSource.CONTENT.name());
        RedirectUtils.getInstance().startActivity(getContext(), SearchActivity.class, bundle);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.content_list_divider));
        binding.contentListRv.addItemDecoration(divider);
        binding.contentListRv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ContentCenterListAdapter(getContext());
        adapter.setItems(pageHandler.getItems());
        binding.contentListRv.setAdapter(adapter);

        pageHandler.refresh();
    }

    private PageHandler<ProductItem> pageHandler = new PageHandler<ProductItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            ProductListParams params = new ProductListParams();
            params.setPage(page);
            params.setPageSize(size);
            params.setContentStatus(Arrays.asList("self_created_approved", "others_created_approved"));
            productListRequest.request(getContext(), ContentCenterFragment.this, params, false);
        }
    };

    private ProductListRequest productListRequest = new ProductListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onProductListSuccess(List<ProductItem> data, Pagination pagination) {
            if (pageHandler.isEmptyData(data, pagination)) {
                binding.refreshListSrl.setVisibility(View.GONE);
                binding.emptyLl.setVisibility(View.VISIBLE);
            } else {
                binding.refreshListSrl.setVisibility(View.VISIBLE);
                binding.emptyLl.setVisibility(View.GONE);
                pageHandler.loadDone(binding.refreshListSrl, pagination, data);
                adapter.notifyDataSetChanged();
            }
        }
    };

    @SubscribeEBus(receiveKey = EBusConstants.refreshContentList)
    public void onRefreshContentListEvent() {
        binding.refreshListSrl.autoRefresh();
    }
}
